package apis

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/api"
	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"gitee.com/sansaniot/ssiot-core/serrors"
	"github.com/gin-gonic/gin"
	"ssadmin/common/constant"
	"ssadmin/common/utils"
	"ssadmin/internal/admin/models"
	"ssadmin/internal/admin/models/command"
	"ssadmin/internal/admin/models/query"
	"ssadmin/internal/admin/service"
	cache "ssadmin/internal/admin/utils"
	"time"
)

type Role struct {
	api.Api
}

// FindList 获取Role列表， 调用关系为api->model
func (e Role) FindList(c *gin.Context) {
	// 实例化model和查询条件
	req := query.RoleQuery{}
	m := models.Role{}

	// 向model中注入orm
	err := e.MakeContext(c).Bind(&req).MakeModel(&m.Model).Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	// 获取当前用户
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	if userId != constant.AdminUser {
		req.CreatorId = userId
	}

	// 查库
	list := make([]models.Role, 0)
	var count int64
	err = m.FindList(&req, &list, &count)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	// 返回角色列表
	e.Page(count, req.GetPageSize(), req.GetPageIndex(), list)
}

// FindRecurList 获取Role列表， 调用关系为api->model
func (e Role) FindRecurList(c *gin.Context) {
	// 实例化model和查询条件
	req := query.RoleQuery{}
	m := models.Role{}

	// 向model中注入orm
	err := e.MakeContext(c).Bind(&req).MakeModel(&m.Model).Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	// 获取当前用户
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	req.CreatorId = userId
	// 查库
	list := make([]models.Role, 0)
	var count int64
	err = m.FindRecurList(&req, &list, &count)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	// 返回角色列表
	e.Data(list)
}

// Get 获取Role
func (e Role) Get(c *gin.Context) {
	// 实例化model和查询条件
	req := query.RoleGetReq{}
	m := models.Role{}

	// 向model中注入orm
	err := e.MakeContext(c).Bind(&req).MakeModel(&m.Model).Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	// 查库
	err = m.Get(&req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	// 返回
	e.Data(utils.If(len(m.RoleId) == 0, nil, m))
}

func (e Role) GetMenuIdList(c *gin.Context) {
	req := query.RoleGetReq{}
	s := service.Role{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	if len(req.Id) == 0 {
		userId := user.Get(c, constant.CacheKeyUserId).(string)
		req.Id = cache.GetUserRoleIds(userId)[0]
	}

	var menuIdList []string
	_ = s.GetMenuIdList(&req, &menuIdList)
	e.Data(menuIdList)
}

// Insert 创建Role
func (e Role) Insert(c *gin.Context) {
	req := &command.RoleInsertReq{}
	s := &service.Role{}
	r := &models.Role{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(req).
		MakeModel(&r.Model).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}

	s.SetRoleModel(r)
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	userName := user.Get(c, constant.CacheKeyUserName).(string)
	req.Creator = userName
	req.CreatorId = userId
	req.CreateTime = time.Now()
	err = s.Insert(req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Data(req.GetId())
}

// Update 修改Role
func (e Role) Update(c *gin.Context) {
	req := &command.RoleUpdateReq{}
	s := service.Role{}

	err := e.MakeContext(c).
		MakeOrm().
		Bind(req).
		MakeService(&s.Service).
		Errors

	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.InvalidParam)
		return
	}

	req.SystemId = c.GetHeader(constant.CacheKeySystemId)
	err = s.Update(req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}

	e.Data(req.GetId())
}

// Delete 删除Role
func (e Role) Delete(c *gin.Context) {
	s := service.Role{}
	req := command.RoleDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.InvalidParam)
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	e.Data(req.GetId())
}
